package com.Arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @Author 小闽
 * @Date 2024/10/8 16:20
 * @PackageName: com.Arrays_
 * @ClassName: ArraysSortCustom
 * @Description: 冒泡排序升级版
 * @Version 1.0
 */
public class ArraysSortCustom {
    public static void main(String[] args) {
        Integer[] arr = {-1,-10,67,35,62};
//        bubbleSort(arr);
        bubbleSort1(arr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
               int i1 =  (Integer)o1;
               int i2 = (Integer)o2;
                return i2 - i1;
            }
        });
        System.out.println("===排序后的数组===");
        System.out.println(Arrays.toString(arr));
    }
    public static void bubbleSort(Integer arr[]){
        int temp = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length -i - 1; j++){
                // 从小到大排序
                if (arr[j] > arr[j + 1]){
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    // 定制冒泡排序
    public static void bubbleSort1(Integer arr[], Comparator c){
        int temp = 0;
        for (int i = 0; i < arr.length -1; i++) {
            for (int j = 0; j < arr.length -i - 1; j++){
                // 从小到大排序
                // 在这里 arr[j] > arr[j + 1] 的结果会直接影响到排序的结果
                if (c.compare(arr[j], arr[j+1]) > 0){
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j+1] = temp;
                }
            }
        }
    }
}
